﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace PPI.Data.Utility
{
    public class DRemedy 
    {
        public static void Update(Int32 crossRemedyId,Int32 remedyStatusFk,string remedyAssigned,DateTime remedyLastAction,DateTime remedyResolvedDate,string remedyNotes,string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Utility_Asset_XMissionStructure_Poles_CrossRemedy_Vendor_InspectionVisit_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;


                cmd.Parameters.Add(DBHelper.CreateIntParameter("@CrossRemedyId", ParameterDirection.Input, false, crossRemedyId));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@RemedyStatusFk", ParameterDirection.Input, false, remedyStatusFk));

                if(string.IsNullOrEmpty(remedyAssigned))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@RemedyAssigned",150, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@RemedyAssigned", 150, ParameterDirection.Input, false, remedyAssigned));

                if (remedyLastAction == DateTime.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@RemedyLastAction", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@RemedyLastAction", ParameterDirection.Input, false, remedyLastAction));

                if (remedyResolvedDate == DateTime.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@RemedyResolvedDate", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@RemedyResolvedDate", ParameterDirection.Input, false, remedyResolvedDate));

                if (string.IsNullOrEmpty(remedyNotes))
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@RemedyNotes", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@RemedyNotes", ParameterDirection.Input, false, remedyNotes));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static DataSet SelectAll(string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Utility_Asset_XMissionStructure_Poles_CrossRemedy_Vendor_InspectionVisit_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Utility_Asset_XMissionStructure_Poles_CrossRemedy_Vendor_InspectionVisit_SelectAll", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }
    }
}
